/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.euitio.jmplayer.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import org.euitio.jmplayer.controller.MPlayer;
import org.euitio.jmplayer.controller.PositionChangedListener;
import org.euitio.jmplayer.model.PositionChangedEvent;

/**
 *
 * @author Carballude
 */
public class MainWindow extends JFrame implements PositionChangedListener{

    private JButton btPlayPause;
    private JSlider sliderSeek;

    private JPanel controlPanel;

    private JMenuBar menuBarCustom;
    private JMenu menuFile;
    private JMenuItem menuFileOpen;
    private JMenuItem menuFileExit;

    public MainWindow() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
        setTitle("JMPlayer, an extremely simple interface");
        setSize(640,480);
        setVisible(true);
        getContentPane().setLayout(new BorderLayout());
        add(getMenuBarCustom(),BorderLayout.NORTH);
        add(getControlPanel(),BorderLayout.SOUTH);
        addWindowListener(new WindowListener() {

            @Override
            public void windowOpened(WindowEvent e) {}

            @Override
            public void windowClosing(WindowEvent e) {
                MPlayer.getInstance().closePlayer();
            }

            @Override
            public void windowClosed(WindowEvent e) { }

            @Override
            public void windowIconified(WindowEvent e) {}

            @Override
            public void windowDeiconified(WindowEvent e) {}

            @Override
            public void windowActivated(WindowEvent e) {}

            @Override
            public void windowDeactivated(WindowEvent e) {}
        });
        MPlayer.getInstance().subscribeToPositionChangeEvents(this);

    }

    private JMenuItem getMenuFileExit(){
        if(menuFileExit==null){
            menuFileExit = new JMenuItem("Exit",'e');
        }
        return menuFileExit;
    }

    private JMenuItem getMenuFileOpen(){
        if(menuFileOpen == null){
            menuFileOpen = new JMenuItem("Open...",'o');
            menuFileOpen.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    openFileAction();
                }
            });
        }
        return menuFileOpen;
    }

    private JMenu getMenuFile(){
        if(menuFile==null){
            menuFile = new JMenu("File");
            menuFile.setMnemonic('f');
            menuFile.add(getMenuFileOpen());
            menuFile.addSeparator();
            menuFile.add(getMenuFileExit());
        }
        return menuFile;
    }

    private JMenuBar getMenuBarCustom() {
        if(menuBarCustom == null){
            menuBarCustom = new JMenuBar();
            menuBarCustom.add(getMenuFile());
        }
        return menuBarCustom;
    }

    private JButton getBtPlayPause(){
        if(btPlayPause==null){
            btPlayPause = new JButton("Play");
        }
        return btPlayPause;
    }

    private JSlider getSliderSeek(){
        if(sliderSeek==null){
            sliderSeek = new JSlider(SwingConstants.HORIZONTAL,0,100,0);
        }
        return sliderSeek;
    }

    private JPanel getControlPanel(){
        if(controlPanel==null){
           controlPanel = new JPanel(new BorderLayout());
           controlPanel.add(getBtPlayPause(),BorderLayout.WEST);
           controlPanel.add(getSliderSeek(),BorderLayout.CENTER);
        }
        return controlPanel;
    }

    private void openFileAction(){
        JFileChooser fc = new JFileChooser();
        if(fc.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
            MPlayer.getInstance().loadFile(fc.getSelectedFile().getAbsolutePath());
    }

    @Override
    public void positionChanged(PositionChangedEvent positionChangedEvent) {
        sliderSeek.setValue(positionChangedEvent.getPercent());
    }

}
